package com.fmdj.mis.api.service.Impl;

import cn.hutool.core.map.MapUtil;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.db.dao.DeptDao;
import com.fmdj.mis.api.db.pojo.DeptEntity;
import com.fmdj.mis.api.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public PageUtils searchDeptByPage(Map param) {
        //计算起始索引
        Integer page = MapUtil.getInt(param, "page");
        Integer length = MapUtil.getInt(param, "length");
        Integer start = (page - 1) * length;
        param.put("start", start);

        ArrayList<HashMap> list = deptDao.searchDeptByPage(param);

        long count = deptDao.searchDeptCount(param);

        return new PageUtils(list,count,page,length);
    }

    @Override
    public ArrayList<HashMap> searchAllDept() {
        return deptDao.searchAllDept();
    }

    @Override
    public HashMap searchById(int id) {
        return deptDao.searchById(id);
    }

    @Override
    public int insert(DeptEntity dept) {
        return deptDao.insert(dept);
    }

    @Override
    public int update(DeptEntity dept) {
        return deptDao.update(dept);
    }

    @Override
    public int deleteDeptByIds(Integer[] ids) {

        Integer rows = null;

        if (!deptDao.searchCanDelete(ids)) {
            rows = deptDao.deleteDeptByIds(ids);
        }

        return rows;
    }
}
